'''
Created on Sep 8, 2010

@author: dexen
'''
import os 
import sys
import random

#DEVELOPMENT
def enableHouModule():
    '''Set up the environment so that "import hou" works.'''
    # Importing hou will load in Houdini's libraries and initialize Houdini.
    # In turn, Houdini will load any HDK extensions written in C++.  These
    # extensions need to link against Houdini's libraries, so we need to
    # make sure that the symbols from Houdini's libraries are visible to
    # other libraries that Houdini loads.  So, we adjust Python's dlopen
    # flags before importing hou.
    if hasattr(sys, "setdlopenflags"):
        old_dlopen_flags = sys.getdlopenflags()
        import DLFCN
        sys.setdlopenflags(old_dlopen_flags | DLFCN.RTLD_GLOBAL)

    try:
        import hou
    except ImportError:
        # Add $HFS/houdini/python2.5libs to sys.path so Python can find the
        # hou module.
        sys.path.append( os.environ['HFS'] + "/houdini/python%d.%dlibs" % sys.version_info[:2])
        import hou
    finally:
        if hasattr(sys, "setdlopenflags"):
            sys.setdlopenflags(old_dlopen_flags)
            
#houdini geometry
def houdini(genotypes, basefilepath, filename, count):
    enableHouModule()
    import hou
    basefile = os.path.join(basefilepath, filename)
    hou.hipFile.load(basefile)
    genotype = hou.node("/obj/geo1/genotypes")
    for cnt in range(len(genotypes)):
        genotype.setParms({"genotype"+str(cnt+1):genotypes[cnt]})
        
    hou.hipFile.save()
        
    ep_geom = os.path.join(basefilepath, "ep_geom")
    if not os.path.isdir(ep_geom):
        os.mkdir(ep_geom)
    rad_geom = os.path.join(basefilepath, "rad_geom")
    if not os.path.isdir(rad_geom):
        os.mkdir(rad_geom)
    display_geom = os.path.join(basefilepath, "display_geom")
    if not os.path.isdir(display_geom):
        os.mkdir(display_geom)
    sensor_geom = os.path.join(basefilepath, "sensor_geom")
    if not os.path.isdir(sensor_geom):
        os.mkdir(sensor_geom)
        
    hou.node("/obj/geo1/ep_geom").geometry().saveToFile(ep_geom + "/houseschema_e" + count + ".bgeo")
    #hou.node("/obj/geo1/rad_geom").geometry().saveToFile(rad_geom + "/houseschema" + count + ".bgeo")
    #hou.node("/obj/geo1/display_geom").geometry().saveToFile(display_geom + "/houseschema" + count + ".bgeo")
    #hou.node("/obj/geo1/sensor_geom").geometry().saveToFile(sensor_geom + "/sensor" + count + ".bgeo")


def sim_houdini(basefilepath, filename, frame):
    enableHouModule()
    import hou
    basefile = os.path.join(basefilepath, filename)
    
    ep_result_geom = os.path.join(basefilepath, "ep_result_geom")
    if not os.path.isdir(ep_result_geom):
        os.mkdir(ep_result_geom)
    rad_result_geom = os.path.join(basefilepath, "rad_result_geom")
    if not os.path.isdir(rad_result_geom):
        os.mkdir(rad_result_geom)
        
    hou.hipFile.load(basefile)
    hou.node("obj/geo1/merge1").cook(force = False, frame_range = (1,1))
#development
genotypes = [0.91353061323993912, 0.60811869442124045, 0.64689643263069019, 0.47021178735480162, 0.24329624602866429, 0.86780722233200069, 0.35883526391397469, 0.10735966526107943, 0.30373087316942593, 0.9292073957607887, 0.087458442147427817, 0.2857861140911957, 0.096566124217889904, 0.31445591300408426, 0.45327969415365821, 0.45179798162090723, 0.47486823656994703, 0.43640847696601692, 0.89477820823946597, 0.97301201804271964, 0.24818024690727059, 0.93777489311991791, 0.10131066795077681, 0.14581956425045484, 0.30095267273934989, 0.5778425594927582, 0.53994109519507316, 0.082023955138089133, 0.6825416078343447, 0.19658740804981578, 0.19947644680587184, 0.75203535846489367, 0.90549834782265726, 0.83612249742484412, 0.919174901422803, 0.41189937968717893, 0.83442845980405334, 0.015561168378769308, 0.19661484648238936, 0.50499936594168282, 0.17774082398991498, 0.34007175546828783, 0.26459134158991937, 0.50998506901328022, 0.84349024928175331, 0.70286446854413154, 0.0050613593784308852, 0.21693890593826881, 0.45066780259830652, 0.039003087929883162, 0.10538375448433901, 0.50392452075841665, 0.35844193619497333, 0.27354177756843012, 0.58217780669885977, 0.45405924746319981]
print len(genotypes)

basefilepath = "/home/dexen/workspace/house_schema/houdinibasefile"
filename = "houseschema.hipnc"
count = str(0)
houdini(genotypes, basefilepath, filename, count)


#evaluation
s_basefilepath = "/home/dexen/workspace/house_schema/houdinibasefile"
s_filename = "sim_ep.hipnc"
sim_houdini(s_basefilepath, s_filename, 0)



    